
import { _decorator, Component, Sprite, PolygonCollider2D, Vec2, Node, Vec3, v2, v3, UITransform } from 'cc';
import { HoleAction } from './game/HoleAction.js';
import { Tools } from './game/Tools.js';
const { ccclass, property, executeInEditMode } = _decorator;

@ccclass('editPolyCol')
@executeInEditMode(true)
export class editPolyCol extends Component {

    target: Node = null;

    collider: PolygonCollider2D = null;

    offset_x = 0
    offset_y = 0

    @property(Boolean)
    isRestart = false;

    start() {
        this.collider = this.getComponent(PolygonCollider2D);
        if(!this.collider){
            this.collider = this.addComponent(PolygonCollider2D);
            this.collider.threshold = 5
        }
    }


    update(dt: number) {
        if (this.isRestart) {

            this.run();
            this.isRestart = false
        }
    }


    run() {

        this.target = this.node.parent;

        console.log("edit load")
        console.log(this.collider.points)

        let poss: Vec3[] = []
        this.target.children.forEach(element => {
            let b = element.name.indexOf("hole")
            if (b >= 0) {
                poss.push(element.getPosition())

                const size = element.getComponent(UITransform).contentSize;
                this.offset_x = size.width / 2;
                this.offset_y = size.height / 2;

                console.log("size:", size)
            }
        });


        this.jisPos(poss)

        let poly = this.target.getComponent(PolygonCollider2D);
        if (!poly)
            poly = this.target.addComponent(PolygonCollider2D)

        poly.points = this.collider.points
        poly.offset = this.collider.offset

        this.collider.sensor = true;
        this.collider.enabled = false;
    }

    jisPos(poss: Vec3[]) {
        console.log("点：", this.collider.points.length)
        console.log(this.offset_x)
        console.log(this.offset_y)


        let pos_indexs: number[] = []

        for (let i = 0; i < poss.length; i++) {

            let new_poss: Vec2[] = []

            let pos = poss[i];

            let cur_index = 0
            let cur_min_dis = 99999

            for (let j = 0; j < this.collider.points.length; j++) {
                const pos2 = this.collider.points[j];

                let dis = Vec2.distance(pos, this.V2ToV3(pos2))
                if (dis < cur_min_dis) {
                    cur_min_dis = dis
                    cur_index = j
                }

            }

            pos_indexs.push(cur_index)

            new_poss = this.return_zs_or_yx(new_poss, cur_index, pos);

            let add_index = cur_index;
            let _length = new_poss.length;
            for (let k = 0; k < _length; k++) {
                const item = new_poss[0];
                this.collider.points.splice(add_index, 0, item)
                add_index++;
                new_poss.splice(0, 1)
            }


        }

        console.log("点：", this.collider.points.length)

        console.log(this.collider.points)
    }

    return_zs_or_yx(new_poss: Vec2[], cur_index, pos) {
        let cval = 1

        new_poss.push(v2(this.collider.points[cur_index].x, this.collider.points[cur_index].y));

        const zsPos = v2(pos.x - this.offset_x, pos.y + this.offset_y);

        if (this.collider.points[cur_index].x < zsPos.x || this.collider.points[cur_index].y > zsPos.y) {
            //左上角开始
            new_poss.push(zsPos);
            new_poss.push(v2(pos.x + this.offset_x, pos.y + this.offset_y));
            new_poss.push(v2(pos.x + this.offset_x, pos.y - this.offset_y));
            new_poss.push(v2(pos.x - this.offset_x, pos.y - this.offset_y));
            new_poss.push(v2(pos.x - this.offset_x, pos.y + this.offset_y));
        }
        else {
            //右下角开始
            new_poss.push(v2(pos.x + this.offset_x, pos.y - this.offset_y));
            new_poss.push(v2(pos.x - this.offset_x, pos.y - this.offset_y));
            new_poss.push(v2(pos.x - this.offset_x, pos.y + this.offset_y));
            new_poss.push(v2(pos.x + this.offset_x, pos.y + this.offset_y));
            new_poss.push(v2(pos.x + this.offset_x, pos.y - this.offset_y));
        }


        return new_poss;
    }

    V3ToV2(v: Vec3) { return v2(v.x, v.y) }
    V2ToV3(v: Vec2) { return v3(v.x, v.y, 0) }
}
